package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.persistence.GenericDAO;

public class GenericDAOImpl<T> extends HibernateDaoSupport implements
		GenericDAO<T> {

	@Override
	public T findById(int id, T entityClass) {
		String query = "from "+entityClass.getClass().toString()
				+" where id= ?";
		return (T) getHibernateTemplate().find(query,id);
	}

	@Override
	public List<T> findAll(T entityClass) {
		String query = "from "+entityClass.getClass().toString();
		return (List<T>) getHibernateTemplate().find(query);
	}

	@Override
	public void persist(T obj) {
		getHibernateTemplate().
			saveOrUpdate(obj.getClass().toString(), obj);
		
	}

	@Override
	public void persist(List<T> obj) {
		for(T entity: obj)
			getHibernateTemplate()
				.saveOrUpdate(entity.getClass().toString(), entity);	
	}

	@Override
	public void remove(T obj) {
		getHibernateTemplate()
			.delete(obj.getClass().toString(), obj);
		
	}
}
